package com.cnebula.common.hdfs.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;

import org.apache.hadoop.security.authentication.client.AuthenticationException;


/**
 * ===== HTTP GET 
 * <li>OPEN 打开并读取一个文件
 * <li>GETFILESTATUS  获取文件/ 文件夹的状态信息
 * <li>LISTSTATUS  查看目录列表
 * <li>GETCONTENTSUMMARY 获取目录的上下文环境汇总信息
 * <li>GETFILECHECKSUM 获取文件校验和信息
 * <li>GETHOMEDIRECTORY 获取Home目录
 * <li>GETDELEGATIONTOKEN 获取代理Token
 * <li>GETDELEGATIONTOKENS 获取代理Tokens
 * ===== HTTP PUT 
 * <li>CREATE 创建并写一个文件
 * <li>MKDIRS 创建一个目录
 * <li>CREATESYMLINK 创建文件的符号链接
 * <li>RENAME 重名命文件、文件夹
 * <li>SETREPLICATION 设置备份
 * <li>SETOWNER 设置所有者
 * <li>SETPERMISSION 设置权限
 * <li>SETTIMES 设置文件修改时间
 * <li>RENEWDELEGATIONTOKEN 重新设置代理Token
 * <li>CANCELDELEGATIONTOKEN 取消代理Token
 * ===== HTTP POST 
 * APPEND 在文件末尾追加内容
 * ===== HTTP DELETE <br/>
 * DELETE 删除文件
 */
public interface WebHDFSConnection {

	/*
	 * ========================================================================
	 * GET
	 * ========================================================================
	 */
	/**
	 * GETHOMEDIRECTORY 获取Home目录
	 * 
	 * curl -i "http://<HOST>:<PORT>/webhdfs/v1/?op=GETHOMEDIRECTORY"
	 * 
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws AuthenticationException
	 */
	public String getHomeDirectory() throws MalformedURLException, IOException,
			AuthenticationException;

	/**
	 * OPEN 打开并读取一个文件
	 * 
	 * curl -i -L "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=OPEN
	 * [&offset=<LONG>][&length=<LONG>][&buffersize=<INT>]"
	 * 
	 * @param path
	 * @param os
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String open(String path, OutputStream os)
			throws MalformedURLException, IOException, AuthenticationException;

	/**
	 * GETCONTENTSUMMARY 获取目录的上下文环境汇总信息
	 * 
	 * curl -i "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=GETCONTENTSUMMARY"
	 * 
	 * @param path
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws AuthenticationException
	 */
	public String getContentSummary(String path) throws MalformedURLException,
			IOException, AuthenticationException;

	/**
	 * LISTSTATUS  查看目录列表
	 * 
	 * curl -i "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=LISTSTATUS"
	 * 
	 * @param path
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws AuthenticationException
	 */
	public String listStatus(String path) throws MalformedURLException,
			IOException, AuthenticationException;

	/**
	 * GETFILESTATUS  获取文件/ 文件夹的状态信息
	 * 
	 * curl -i "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=GETFILESTATUS"
	 * 
	 * @param path
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws AuthenticationException
	 */
	public String getFileStatus(String path) throws MalformedURLException,
			IOException, AuthenticationException;

	/**
	 *GETFILECHECKSUM 获取文件校验和信息
	 * 
	 * curl -i "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=GETFILECHECKSUM"
	 * 
	 * @param path
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws AuthenticationException
	 */
	public String getFileCheckSum(String path) throws MalformedURLException,
			IOException, AuthenticationException;

	/*
	 * ========================================================================
	 * PUT
	 * ========================================================================
	 */
	/**
	 * CREATE 创建并写一个文件
	 * 
	 * curl -i -X PUT "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=CREATE
	 * [&overwrite=<true|false>][&blocksize=<LONG>][&replication=<SHORT>]
	 * [&permission=<OCTAL>][&buffersize=<INT>]"
	 * 
	 * @param path
	 * @param is
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws AuthenticationException
	 */
	public String create(String path, InputStream is)  throws MalformedURLException, IOException, AuthenticationException;

	/**
	 * MKDIRS 创建一个目录
	 * 
	 * curl -i -X PUT
	 * "http://<HOST>:<PORT>/<PATH>?op=MKDIRS[&permission=<OCTAL>]"
	 * 
	 * @param path
	 * @return
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String mkdirs(String path) throws MalformedURLException,
			IOException, AuthenticationException;
	
	/**
	 * CREATESYMLINK 创建文件的符号链接
	 * 
	 * curl -i -X PUT "http://<HOST>:<PORT>/<PATH>?op=CREATESYMLINK
	 * &destination=<PATH>[&createParent=<true|false>]"
	 * 
	 * @param path
	 * @return
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String createSymLink(String srcPath, String destPath)
			throws MalformedURLException, IOException, AuthenticationException;

	/**
	 * RENAME 重名命文件、文件夹
	 * 
	 * curl -i -X PUT "http://<HOST>:<PORT>/<PATH>?op=RENAME
	 * &destination=<PATH>[&createParent=<true|false>]"
	 * 
	 * @param path
	 * @return
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String rename(String srcPath, String destPath)
			throws MalformedURLException, IOException, AuthenticationException;

	/**
	 * SETPERMISSION 设置权限
	 * 
	 * curl -i -X PUT "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=SETPERMISSION
	 * [&permission=<OCTAL>]"
	 * 
	 * @param path
	 * @return
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String setPermission(String path) throws MalformedURLException,
			IOException, AuthenticationException;

	/**
	 * SETOWNER 设置所有者
	 * 
	 * curl -i -X PUT "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=SETOWNER
	 * [&owner=<USER>][&group=<GROUP>]"
	 * 
	 * @param path
	 * @return
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String setOwner(String path) throws MalformedURLException,
			IOException, AuthenticationException;

	/**
	 *SETREPLICATION 设置备份
	 * 
	 * curl -i -X PUT "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=SETREPLICATION
	 * [&replication=<SHORT>]"
	 * 
	 * @param path
	 * @return
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String setReplication(String path) throws MalformedURLException,
			IOException, AuthenticationException;

	/**
	 *SETTIMES 设置文件修改时间
	 * 
	 * curl -i -X PUT "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=SETTIMES
	 * [&modificationtime=<TIME>][&accesstime=<TIME>]"
	 * 
	 * @param path
	 * @return
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String setTimes(String path) throws MalformedURLException,
			IOException, AuthenticationException;

	/*
	 * ========================================================================
	 * POST
	 * ========================================================================
	 */
	/**
	 * APPEND 在文件末尾追加内容
	 * curl -i -X POST
	 * "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=APPEND[&buffersize=<INT>]"
	 * 
	 * @param path 文件路径
	 * @param is 
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws AuthenticationException
	 */
	public String append(String path, InputStream is)
			throws MalformedURLException, IOException, AuthenticationException;

	/*
	 * ========================================================================
	 * DELETE
	 * ========================================================================
	 */
	/**
	 * DELETE 删除文件
	 * 
	 * curl -i -X DELETE "http://<host>:<port>/webhdfs/v1/<path>?op=DELETE
	 * [&recursive=<true|false>]"
	 * 
	 * @param path
	 * @return
	 * @throws AuthenticationException
	 * @throws IOException
	 * @throws MalformedURLException
	 */
	public String delete(String path) throws MalformedURLException,
			IOException, AuthenticationException;

}
